window.onload = function () {
    var bbcode = document.getElementById('bb_code');
    var bbcodea = bbcode.getElementsByTagName('a');
    var textarea = document.getElementsByTagName('textarea')[0];
    var select = document.getElementById('font-size');
    var selectd = select.options[select.options.selectedIndex].value;
    var font_color_block = document.getElementById('font_color_list');
    var background_color_block = document.getElementById('background_color_list');
	var font_color_list = font_color_block.getElementsByTagName("span");
    var background_color_list = background_color_block.getElementsByTagName("span");
	var html = document.getElementsByTagName('html')[0];
    var smile = document.getElementById('ubb-smile').getElementsByTagName('img');

    //click buttons to insert bbcodecode
    select.onchange = function(){
      if(select.value=='Tiny') {
        tag('[size=50]','[/size]');
      }else if(select.value=='Small'){
        tag('[size=85]','[/size]');
      }else if(select.value=='Large'){
        tag('[size=150]','[/size]');
      }else if(select.value=='Huge'){
        tag('[size=200]','[/size]');
      }
      
      select.value=selectd;
    }
    
    bbcodea[0].onmousedown  = function() {
        tag('[b]','[/b]');
    };

    bbcodea[1].onmousedown  = function() {
        tag('[i]','[/i]');
    };
    
    bbcodea[2].onmousedown  = function() {
        tag('[u]','[/u]');
    };
        
    //show font-color list
    bbcodea[3].onmouseover = function() {
        font_color_block.style.display = 'block';
    };
    //show background-color list
    bbcodea[4].onmouseover = function() {
        background_color_block.style.display = 'block';
    };
    
    //close palette
	html.onclick = function () {
		font_color_block.style.display = 'none';
		background_color_block.style.display = 'none';
	};
    
    bbcodea[5].onmousedown = function() {
        var url = prompt('Enter the URL','http://');
        if(url){
            if (/^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/.test(url)) {
                tag('[url]'+url,'[/url]');
            }else{
                alert('Invalid URL! URL must begin with http,https,ftp etc.\r\n\r\ne.g. http://www.website.com');
            }
        }
    };
    
    bbcodea[6].onmousedown = function() {
        tag('[quote]','[/quote]');
    };
    
    bbcodea[7].onmousedown = function() {
        tag('[code]','[/code]');
    };
    
    bbcodea[8].onmousedown = function() {
        var img = prompt('Enter the image URL','http://');
        if(img){
            if (/^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?/.test(img)) {
                tag('[img]'+img,'[/img]');
            }else{
                alert('Invalid image URL! Image URL must begin with http or https.\r\n\r\ne.g. http://www.website.com/image.jpg');
            }
        }
    };
    
    bbcodea[9].onmousedown = function() {
        var flash = prompt('Enter the flash URL','http://');
        if(flash){
            if (/^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?(\.swf)$/.test(flash)) {
                tag('[flash]'+flash,'[/flash]');
            }else{
                alert('Invalid flash URL! Flash URL must begin with http or https and ends with .swf.\r\n\r\ne.g. http://www.website.com/flash.swf');
            }       
        }
    };

    // add tags function
    function tag(tag1,tag2){
	     // Code for IE
  		if (document.selection){
            textarea.focus();
            var sel = document.selection.createRange();
            //alert(sel.text);
            sel.text = tag1 + sel.text + tag2;
        }else{  
            // Code for Mozilla Firefox
    		var len = textarea.value.length;
    	    var start = textarea.selectionStart;
    		var end = textarea.selectionEnd;
    		
    		
    		var scrollTop = textarea.scrollTop;
    		var scrollLeft = textarea.scrollLeft;
    
    		
            var sel = textarea.value.substring(start, end);
            //alert(sel);
    		var rep = tag1 + sel + tag2;
            textarea.value =  textarea.value.substring(0,start) + rep + textarea.value.substring(end,len);
    		
    		textarea.scrollTop = scrollTop;
    		textarea.scrollLeft = scrollLeft;
    	}
    }

    //detect if webbrower is firefox
    if(navigator.appName.indexOf("Explorer") > -1){
        for (var i = 0; i < font_color_list.length; i++) {    
            font_color_list[i].onmousedown = function() {    
                tag('[color='+this.innerText+']','[/color]');
            }      
        }
        
        for (var i = 0; i < background_color_list.length; i++) {    
            background_color_list[i].onmousedown = function() {    
                tag('[bgcolor='+this.innerText+']','[/bgcolor]');
            }      
        }              
    }else{
         for (var i = 0; i < font_color_list.length; i++) {    
            font_color_list[i].onmousedown = function() {    
                tag('[color='+this.textContent+']','[/color]');
            }      
         }  
         
         for (var i = 0; i < background_color_list.length; i++) {    
            background_color_list[i].onmousedown = function() {    
                tag('[bgcolor='+this.textContent+']','[/bgcolor]');
            }      
         }      
    }
    
    //insert smiles
    for (var i = 0; i < smile.length; i++) {    
        smile[i].onmousedown = function() {    
            tag(this.alt,'');
        }      
    }
};

    
